package com.qfedu.service01.serviceApi;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.commons.bean.User;
import com.qfedu.commons.bean.result.Result;
import com.qfedu.commons.bean.result.ResultEnum;
import com.qfedu.commons.mapper.UserMapper;
import com.qfedu.commons.serviceApi.UserService;
import com.qfedu.commons.utils.JWTUtil;
import com.qfedu.commons.utils.MD5Builder;
import com.qfedu.service01.utils.RedisUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author No.1
 * @since 2022-07-04
 */
@Service(timeout = 1000000)
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${spring.SnowFlake.data.center.id}")//雪花算法
    private Long centerId;
    @Value("${spring.SnowFlake.data.worker.id}")
    private Long workerId;


    @Override
    public User selectById(Long id) {

        return userMapper.selectById(id);

    }

    @Override//登录
    public Result login(User user) {
        //密码转成md5
        user.setPassword(MD5Builder.create(user.getPassword()));

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.eq("password",user.getPassword());
        wrapper.eq("username",user.getUsername());


        User rightUser = userMapper.selectOne(wrapper);

        if (rightUser==null){
            return null;
        }

        //2生成uuid
        String uuid = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10);
        //3 保存到redis里
        redisUtil.set(uuid,rightUser);
        //4 根据uuid生成token
        Map<String,String> map = new HashMap<>();
        map.put("uuid",uuid);

        String token = JWTUtil.createToken(map);


        //5 把字符串返回去：结果集
        Result rs = new Result(ResultEnum.SELECT_SUCCESS);
        rs.setMsg(token);
        rs.setDatas(JSON.toJSONString(rightUser));

        return rs;
    }

    @Override
    public int register(User user) {
        //雪花算法生成ID
        IdWorker.initSequence(workerId,centerId);
        long id = IdWorker.getId();
        user.setId(id);
        //密码加密
        user.setPassword(MD5Builder.create(user.getPassword()));

        //默认头像
        user.setImageUrl("http://rdngu97lg.hb-bkt.clouddn.com/11.jpg");
        return userMapper.insert(user);
    }

    @Override
    public boolean hasUsername(String username) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);

        User user = userMapper.selectOne(queryWrapper);
        return user != null;
    }

    @Override
    public int update(User user) {

        return userMapper.updateById(user);

    }
}
